package filter.servletcontext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class GetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取上下文
        ServletContext context = this.getServletContext();
        //获取Servlet_Context传来的内容（共享数据）
        String str =(String)context.getAttribute("str");

        //显示类型
        resp.setContentType("text/html");
        //设置编码
        resp.setCharacterEncoding("utf-8");
        //向网页输出内容
        resp.getWriter().print("<h1>"+str+"</h1>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
